<template>
  <transition name="el-fade-in" @after-leave="emit('unlock')">
    <div v-show="show" class="lock-warpper">
      <div class="bg"></div>
      <div class="lock-view">
        <div class="lock-container">
          <el-avatar src="http://comicui.cn/image/avatar.png" :size="120" />
          <Lock :size="20" class="mt-20" />
          <div class="p">屏幕已锁定</div>
          <div class="inputs" :class="{ error: isError }">
            <input v-model="pwd" type="password" autocomplete="off" />
            <button @click="unlock">
              <Unlock class="mr-4" />
              <span>点击解锁</span>
            </button>
            <transition name="el-zoom-in-top">
              <span class="error" v-show="isError">密码错误!</span>
            </transition>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { Lock, Unlock } from '@icon-park/vue-next';
import { ref, watch } from 'vue';

const props = defineProps({
  lock: Boolean
});

const emit = defineEmits(['unlock']);

const show = ref(false);
const pwd = ref('123456');
const isError = ref(false);

function unlock() {
  if (pwd.value !== '123456') {
    isError.value = true;
    return;
  }
  isError.value = false;
  pwd.value = '';
  show.value = false;
}

watch(
  () => props.lock,
  (val) => {
    if (val) {
      show.value = true;
    }
  },
  { immediate: true }
);
</script>

<style scoped lang="scss">
.lock-warpper {
  position: fixed;
  z-index: 99997;
}
.bg {
  position: fixed;
  z-index: 99998;
  inset: 0;
  background: url(http://comicui.cn/image/lock-bg.jpg);
}
.lock-view {
  position: fixed;
  inset: 0;
  z-index: 99999;
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;

  .lock-container {
    width: 560px;
    height: 450px;
    background-color: rgba(255, 255, 255, 0.5);
    backdrop-filter: blur(10px);
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .p {
      margin-top: 10px;
      font-size: 20px;
      font-weight: 900;
    }

    .inputs {
      margin-top: 20px;
      overflow: hidden;
      position: relative;
      display: flex;
      padding-bottom: 18px;

      input {
        padding: 0 8px;
        outline: none;
        height: 40px;
        width: 280px;
        font-size: 18px;
        border-radius: 4px 0 0 4px;
        border: 1px solid var(--el-color-primary);
      }
      button {
        border: 0;
        outline: none;
        height: 40px;
        padding: 0 8px;
        background-color: var(--el-color-primary);
        color: #fff;
        cursor: pointer;
        border-radius: 0 4px 4px 0;
        display: inline-flex;
        align-items: center;
      }

      .error {
        position: absolute;
        bottom: 0;
        left: 0;
        font-size: 12px;
        color: #f00;
      }
    }
    .inputs.error {
      input {
        border-color: #f00;
      }
    }
  }
}
</style>
